<!--
 * @Description: 退款管理
 * @Author: xiao li
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2022-07-21 15:12:50
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-shop-refund">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="通知类型" prop="type">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.type"
              placeholder="请选择"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="阅读状态" prop="have_look">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.have_look"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column
          prop="order_id"
          label="订单ID"
          width="120"
        ></el-table-column>
        <el-table-column prop="type" label="消息内容">
          <template slot-scope="scope">
            <div class="flex-y-center">
              {{
                scope.row.type === 1
                  ? '你有一笔新的订单，请及时联系技师处理'
                  : '你有一笔新的退款订单，请及时处理'
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="推送时间"> </el-table-column>
        <el-table-column prop="have_look" label="阅读状态">
          <template slot-scope="scope">
            {{ statusType[scope.row.have_look] }}
          </template>
        </el-table-column>
        <el-table-column min-width="160" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  updateItem(scope.row.id, 1, false),
                    $router.push(
                      scope.row.type === 1
                        ? `/shop/order/detail?id=${scope.row.order_id}`
                        : `/shop/refund/detail?id=${scope.row.order_id}`
                    )
                "
                >{{ $t('action.view') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                v-if="scope.row.have_look === 1"
                >{{ $t('action.delete') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="success"
                @click="updateItem(scope.row.id, 1)"
                v-if="scope.row.have_look === 0"
                >阅读</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import moment from 'moment'
export default {
  data () {
    return {
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      range: '',
      typeOptions: [
        {
          label: '全部',
          value: 0
        },
        {
          label: '订单',
          value: 1
        },
        {
          label: '退款订单',
          value: 2
        }
      ],
      statusOptions: [
        {
          label: '全部',
          value: -1
        },
        {
          label: '未读',
          value: 0
        },
        {
          label: '已读',
          value: 1
        }
      ],
      statusType: {
        0: '未读',
        1: '已读'
      },
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        start_time: '',
        end_time: '',
        type: 0,
        have_look: -1
      },
      tableData: [],
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    ...mapMutations(['changeRoutesItem']),
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let range = JSON.parse(JSON.stringify(this.range))
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      if (range && range.length > 0) {
        searchForm.start_time = parseInt(range[0] / 1000)
        searchForm.end_time = parseInt(range[1] / 1000) + 24 * 3600 - 1
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      if (searchForm.have_look === -1) {
        delete searchForm.have_look
      }
      let { code, data } = await this.$api.shop.noticeList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      })
        .then(() => {
          this.updateItem(id, -1)
        })
        .catch(() => { })
    },
    async updateItem (id, status, show = true) {
      this.$api.shop.noticeUpdate({ id, have_look: status }).then((res) => {
        if (res.code === 200) {
          if (status === 1) {
            this.changeRoutesItem({ key: 'notice_num', val: this.routesItem.notice_num - 1 })
          }
          if (show) {
            this.$message.success(
              this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper')
            )
          }
          if (status === -1) {
            this.searchForm.page =
              this.searchForm.page <
                Math.ceil((this.total - 1) / this.searchForm.limit)
                ? this.searchForm.page
                : Math.ceil((this.total - 1) / this.searchForm.limit)
          }
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-shop-refund {
  .page-main {
    width: 100%;

    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }
  }
}

.none {
  display: none;
}
</style>
<style media="print">
.none {
  display: block;
}
</style>
